﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Xml.Linq;

namespace uottawatourguide
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            this.Items = new ObservableCollection<TourViewModel>();
        }

        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public ObservableCollection<TourViewModel> Items { get; private set; }

        private string _sampleProperty = "Sample Runtime Property Value";
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding
        /// </summary>
        /// <returns></returns>
        public string SampleProperty
        {
            get
            {
                return _sampleProperty;
            }
            set
            {
                if (value != _sampleProperty)
                {
                    _sampleProperty = value;
                    NotifyPropertyChanged("SampleProperty");
                }
            }
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData()
        {
            string filename = @"sampletourdata.xml";
            XElement tours;
            //if (File.Exists(filename))
            //{
                tours = XElement.Load(filename);
                foreach (var t in tours.Elements("tour"))
                {
                    string thumbnailuri = t.Element("ThumbNailURI").Value;
                    string title = t.Element("title").Value;
                    string description = t.Element("description").Value;
                    this.Items.Add(new TourViewModel() { ThumbNailURI=thumbnailuri, Title = title, Description = description, Stops = t.Element("stops") });

                }
                 this.IsDataLoaded = true;
            //}
            //else
            //{
            //    MessageBox.Show("Could not find file " + filename);
            //    this.IsDataLoaded = false;
            //}

        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}